import * as Cesium from "cesium";
import BaseDraw from "./common/BaseDraw";
import * as CesiumCoord from './common/CesiumCrood'
import GroupEntityCollection from '../../Common/GroupEntityCollection'
import { v4 } from "uuid";
/**
 * 派生类-绘制点
 */
export default class DrawPoint extends BaseDraw {
  constructor(viewer, tip) {
    super(viewer);
    this.viewer = viewer
    this.tip = tip // 提示DOM
    this.groupId // 当前绘制的groupID
    // 事件对象，每种类型的事件只能注册一次
    this.handler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
    this.entityCollection = new GroupEntityCollection(viewer)     // 集合
  }
  getAllEntity() {
    return this.entityCollection.getAllEntities();
  }
  // 点实体
  createPointEntity(cartesian) {
    const entity = new Cesium.Entity({
      position: cartesian, // 世界坐标
      point: {
        color: Cesium.Color.YELLOW,
        pixelSize: 15,
        disableDepthTestDistance: Number.POSITIVE_INFINITY,
      },
    });
    return entity;
  }
  // 标签实体
  createLabelEntity(cartesian) {
    const posList = CesiumCoord.cartesian3ToLonLatHeight(cartesian, 4); // 世界坐标转化为经纬度坐标
    const entity = new Cesium.Entity({
      position: cartesian, // 世界坐标
      label: {
        text: `经度：${posList[0]},纬度：${posList[1]}`,
        font: "20px",
        color: Cesium.Color.RED,
        pixelOffset: new Cesium.Cartesian2(0, -20),
        fillColor: Cesium.Color.WHITE,
        showBackground: true,
        backgroundColor: Cesium.Color.BLUE,
      },
    });
    return entity;
  }
}
/**
 * 开始绘制点
 * @param {* 是否显示测量Label} showMeasureLabel
 * @param {* 是否连续绘制} continuousDraw
 */
DrawPoint.prototype.startDraw = function (
  showMeasureLabel = true,
  continuousDraw = true
) {
  const _this = this;
  this.tip.show("单击开始绘制");
  this.groupId = v4() // 每一次绘制就是一次新的记录
  // 左键单击事件
  this.handler.setInputAction(
    (event) => {
      const cartesian = CesiumCoord.ScreenToCartesian3(_this.viewer, event.position); // 屏幕坐标转世界坐标
      if (!Cesium.defined(cartesian)) return;
      if (showMeasureLabel) {
        // 标注实体
        const labelEntity = _this.createLabelEntity(cartesian);
        _this.entityCollection.add(labelEntity, this.groupId); // 加入集合
      }
      // 点实体
      const pointEntity = _this.createPointEntity(cartesian);
      _this.entityCollection.add(pointEntity, this.groupId); // 加入集合

      if (!continuousDraw) {
        // 不连续绘制
        _this.endDraw();
      }
    },
    Cesium.ScreenSpaceEventType.LEFT_CLICK
  );
  // 鼠标移动事件
  this.handler.setInputAction(
    (event) => {
      const { x, y } = event.endPosition;
      this.tip.moveTipDOMPosition(x, y); // 改变 TipDOM 位置
    },
    Cesium.ScreenSpaceEventType.MOUSE_MOVE
  );
  // 右键单击事件
  this.handler.setInputAction(
    () => {
      _this.endDraw();
    },
    Cesium.ScreenSpaceEventType.RIGHT_CLICK
  );
};

/**
 * 结束绘制点
 */
DrawPoint.prototype.endDraw = function () {
  this.tip.close();
  // 移除绑定的事件
  this.handler.removeInputAction(
    Cesium.ScreenSpaceEventType.LEFT_CLICK
  );
  this.handler.removeInputAction(
    Cesium.ScreenSpaceEventType.MOUSE_MOVE
  );
  this.handler.removeInputAction(
    Cesium.ScreenSpaceEventType.RIGHT_CLICK
  );
};
/**
 * 清除绘制的点
 */
DrawPoint.prototype.removeDraw = function () {
  this.entityCollection.removeAll(); // 清空集合所有
};
